package com.gingkor.common.view.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.os.Build
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.TintTypedArray
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import com.gingko.duominuo.R

class TitleView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = R.attr.toolbarStyle
) : Toolbar(context!!, attrs, defStyleAttr) {
    private var tvTitle: TextView? = null
    private var tvSubtitle: TextView? = null

    init {
        addTitleView(attrs, defStyleAttr)
    }

    @SuppressLint("RestrictedApi", "PrivateResource")
    private fun addTitleView(attrs: AttributeSet?, defStyleAttr: Int) {
        // Need to use getContext() here so that we use the themed context
        val linearLayout = LinearLayout(context)
        val layoutParams =
            LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
            )
        layoutParams.gravity = Gravity.CENTER
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.gravity = Gravity.CENTER
        addView(linearLayout, layoutParams)
        tvTitle = TextView(context).apply {
            gravity = Gravity.CENTER
            textSize = 18f
        }
        linearLayout.addView(tvTitle, layoutParams)
        tvSubtitle = TextView(context).apply {
            gravity = Gravity.CENTER
            textSize = 10f
            visibility = View.GONE
        }
        linearLayout.addView(tvSubtitle, layoutParams)
        val typedArray = TintTypedArray.obtainStyledAttributes(
            context, attrs,
            R.styleable.Toolbar, defStyleAttr, 0
        )

        typedArray.getText(R.styleable.Toolbar_title)?.let {
            title = it
        }
        typedArray.getText(R.styleable.Toolbar_subtitle)?.let {
            subtitle = it
        }
        if (typedArray.hasValue(R.styleable.Toolbar_navigationIcon)) {
            navigationIcon = typedArray.getDrawable(R.styleable.Toolbar_navigationIcon)
        } else {
            setNavigationIcon(R.mipmap.ic_nav_back)
            setNavigationOnClickListener {
                if (context is Activity) {
                    (context as Activity).onBackPressed()
                }
            }
        }
        if (typedArray.hasValue(R.styleable.Toolbar_titleTextColor)) {
            setTitleTextColor(typedArray.getColorStateList(R.styleable.Toolbar_titleTextColor))
        } else {
            setTitleTextColor(ContextCompat.getColor(context, R.color.textPrimary))
        }
        if (typedArray.hasValue(R.styleable.Toolbar_subtitleTextColor)) {
            setSubtitleTextColor(typedArray.getColorStateList(R.styleable.Toolbar_subtitleTextColor))
        }
        typedArray.recycle()
    }

    override fun setTitle(title: CharSequence) {
        tvTitle?.text = title
    }

    override fun setTitle(resId: Int) {
        tvTitle?.setText(resId)
    }

    override fun setSubtitle(resId: Int) {
        tvSubtitle?.setText(resId)
        tvSubtitle?.visibility = View.VISIBLE
    }

    override fun setSubtitle(subtitle: CharSequence) {
        tvSubtitle?.text = subtitle
        tvSubtitle?.visibility = View.VISIBLE
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    override fun setTitleTextAppearance(
        context: Context,
        resId: Int
    ) {
        tvTitle?.setTextAppearance(resId)
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    override fun setSubtitleTextAppearance(
        context: Context,
        resId: Int
    ) {
        tvSubtitle?.setTextAppearance(resId)
    }

    override fun setTitleTextColor(color: Int) {
        tvTitle?.setTextColor(color)
    }

    override fun setTitleTextColor(color: ColorStateList) {
        tvTitle?.setTextColor(color)
    }

    override fun setSubtitleTextColor(color: Int) {
        tvSubtitle?.setTextColor(color)
    }

    override fun setSubtitleTextColor(color: ColorStateList) {
        tvSubtitle?.setTextColor(color)
    }
}